package cc.wfu.domain.order.service.rule.armory;

import cc.wfu.domain.order.model.entity.ProductActivitySkuEntity;
import cc.wfu.domain.order.model.valobj.ActivitySkuStockKeyVO;
import cc.wfu.domain.order.repository.IProductActivityRepository;
import cc.wfu.types.common.Constants;
import cc.wfu.types.common.RedisKey;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: jia-qiang ljq1024.cc
 * @desc: 商品活动sku预热
 * @Date: 2024-06-05-22:23
 */
@Slf4j
@Service
@AllArgsConstructor
public class ProductActivityArmory implements IProductActivityArmory, IProductActivityDispatch {

    private IProductActivityRepository repository;

    /**
     * 活动sku预热
     * @param sku skU ID
     */
    @Override
    public boolean assembleProductActivitySku(Long sku) {
        // 预热活动sku库存【查询】
        ProductActivitySkuEntity activitySkuEntity = repository.queryActivitySku(sku);
        cacheActivitySkuStockCount(sku, activitySkuEntity.getStockCountSurplus());

        // 预热活动【查询时预热到缓存】
        repository.queryProductActivityByProductId(activitySkuEntity.getProductId());
        // todo 预热活动次数【查询时预热到缓存】
//        repository.queryRaffleActivityCountByActivityCountId(activitySkuEntity.getActivityCountId());

        return true;
    }
    /**
     * 缓存 sku库存
     * @param sku sku ID
     * @param stockCount sku 库存
     */
    private void cacheActivitySkuStockCount(Long sku, Integer stockCount) {
        repository.cacheActivitySkuStockCount(sku, stockCount);
    }


    /**
     * 根据商品ID，扣减奖品缓存库存
     * @param activitySkuStockKeyVO 互动SKU商品信息
     * @param endDateTime 活动结束时间，根据结束时间设置加锁的key为结束时间
     */
    @Override
    public boolean subtractionActivitySkuStock(ActivitySkuStockKeyVO activitySkuStockKeyVO, Date endDateTime) {
        return repository.subtractionActivitySkuStock(activitySkuStockKeyVO, endDateTime);
    }
}
